<?php

class chart_ctrl extends Controller
{


    //////每一个都进行单个提交

    public function __construct()
    {
        parent::__construct();

    }

    //增加chart
    public function save_chart()
    {
        if (!$this->ajax_err && $this->uid) {
            $id = (empty($_REQUEST['id']))?  0 : $_REQUEST['id'];
            $name = $_REQUEST["name"];
            $pic = $_REQUEST["pic"];
            $detail_img = $_REQUEST["detail_img"];
            $itemIds = $_REQUEST["itemIds"];
            $order_index = $_REQUEST["order_index"];
            $hot = $_REQUEST["hot"];

            $_mdl = Loader::mdl("chart_mdl");
            $data = $_mdl->add_chart($id, $name,$pic,$detail_img, $itemIds, $order_index, $hot);
            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }

    //获取chart
    public function get_chart()
    {
        if (!$this->ajax_err) {
            $pageNo = (!empty($_REQUEST['pageNo'])) ? (int)($_REQUEST['pageNo']) : 1;
            $pageSize = (!empty($_REQUEST['pageSize'])) ? (int)($_REQUEST['pageSize']) : 40;
            $_mdl = Loader::mdl("chart_mdl");
            $data = $_mdl->get_chart($pageNo, $pageSize);

            $rs = array();
            foreach($data['list'] as $one){
                $ids = json_decode($one['itemIds'],true);

                $one['items'] = array();
                //$one['selectedSKU'] = array();
                foreach($ids as $k=>$v) {
                    $_mdl = Loader::mdl('items_mdl');
                    $item = $_mdl->get_items(1, 5, $k, '');

                    //商品不存在
                    //$temp_item = (object)null;
                    if($item['list'][0]){
                        $temp_item = $item['list'][0];
                        foreach (json_decode($temp_item['skus'],true) as $sku){
                            if($sku['sku_id'] == $v){
                                $temp_item['selectedSKU'] = $sku;
                            }
                        }
                        array_push($one['items'],$temp_item);
                    }



                }
                array_push($rs,$one);
            }

            $data['list'] = $rs;

            $this->ajax($data);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }


    //删除chart
    public function delete_chart()
    {
        if (!$this->ajax_err && $this->uid) {
            $id = $_REQUEST["id"];
            $_mdl = Loader::mdl("chart_mdl");
            $result = $_mdl->delete_chart($id);
            $this->ajax($result);
        } else {
            return $this->ajax('', array(1, '未登录', 1, '请登录'));
        }
    }

}